package com.bocai.freeman.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bocai.freeman.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class OrderDemandAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public enum ITEM_TYPE {
        ITEM_TYPE_COMMON,
        ITEM_TYPE_EVALUATE,
        ITEM_TYPE_REFUND,
    }

    private OnItemClickLitener mOnItemClickLitener;
    private Context mContext;
    private List<Integer> mdatas;

    public OrderDemandAdapter(List<Integer> datas, Context context) {
        this.mdatas = datas;
        this.mContext = context;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position, int itemType);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE.ITEM_TYPE_COMMON.ordinal()) {

            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_common, parent, false);
            return new CommonHolder(view);

        } else if (viewType == ITEM_TYPE.ITEM_TYPE_EVALUATE.ordinal()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_evaluate, parent, false);
            return new EvaluateHolder(view);

        } else if (viewType == ITEM_TYPE.ITEM_TYPE_REFUND.ordinal()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_demand_refund, parent, false);
            return new RefundHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof CommonHolder) {
            if (mOnItemClickLitener != null) {
                ((CommonHolder) holder).llContentCommon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos, 1);
                    }
                });
            }

        } else if (holder instanceof EvaluateHolder) {
            if (mOnItemClickLitener != null) {
                ((EvaluateHolder) holder).llContentEvaluate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos, 2);
                    }
                });
            }
        } else if (holder instanceof RefundHolder) {

            if (mOnItemClickLitener != null) {
                ((RefundHolder) holder).llContentRefund.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos, 3);
                    }
                });
            }
        }

    }


    @Override
    public int getItemViewType(int position) {

        if (mdatas.get(position) == 0) {
            return ITEM_TYPE.ITEM_TYPE_COMMON.ordinal();
        } else if (mdatas.get(position) == 1) {
            return ITEM_TYPE.ITEM_TYPE_EVALUATE.ordinal();
        } else if (mdatas.get(position) == 2) {
            return ITEM_TYPE.ITEM_TYPE_REFUND.ordinal();
        } else {
            return -1;
        }

    }

    @Override
    public int getItemCount() {
        return mdatas.size();
    }


    public class CommonHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ll_content_common)
        LinearLayout llContentCommon;

        public CommonHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public class EvaluateHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ll_content_evaluate)
        LinearLayout llContentEvaluate;

        public EvaluateHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public class RefundHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ll_content_refund)
        LinearLayout llContentRefund;

        public RefundHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}